home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Peter Lewis / FetchNews 1.0.0b / source / MyHandleFile.unit < prev    next >
Encoding:
Text File  |  1993-03-12  |  2.2 KB  |  98 lines  |  [TEXT/PJMM]

  1. unit MyHandleFile;
  2.  
  3. interface
  4.  
  5.     type
  6.         lineEnding = (CL_CR, CL_LF, CL_CRLF);
  7.         HandleFile = record
  8.                 data: handle; { The data }
  9.                 pos: longInt; { current position in handle }
  10.                 crlf: lineEnding; { Only used for output, reading will handle either case }
  11.                 error: OSErr; { Cumulative error }
  12.             end;
  13. { You are free to modify any and all fields, keep 0<=pos<GetHandleSize(data) }
  14.  
  15.     procedure CreateHandleFile (var hf: HandleFile; le: lineEnding);
  16.     procedure DestroyHandleFile (var hf: HandleFile);
  17.     procedure WriteToHandleFile (var hf: HandleFile; s: str255); { Write string at pos into data }
  18.     function ReadFromHandleFile (var hf: HandleFile; var s: str255): boolean;
  19. { Read string from pos in data, update pos, return true unless eohandle }
  20.  
  21. implementation
  22.  
  23.     uses
  24.         MyTypes;
  25.  
  26.     procedure CreateHandleFile (var hf: HandleFile; le: lineEnding);
  27.     begin
  28.         with hf do begin
  29.             data := NewHandle(0);
  30.             pos := 0;
  31.             crlf := le;
  32.             error := MemError;
  33.         end;
  34.     end;
  35.  
  36.     procedure DestroyHandleFile (var hf: HandleFile);
  37.     begin
  38.         DisposeHandle(hf.data);
  39.         hf.data := nil;
  40.     end;
  41.  
  42.     procedure WriteToHandleFile (var hf: HandleFile; s: str255);
  43.         var
  44.             ret: longInt;
  45.     begin
  46.         with hf do begin
  47.             case crlf of
  48.                 CL_CR: 
  49.                     s := concat(s, cr);
  50.                 CL_LF: 
  51.                     s := concat(s, lf);
  52.                 CL_CRLF: 
  53.                     s := concat(s, cr, lf);
  54.             end;
  55.             ret := Munger(data, pos, nil, 0, @s[1], length(s));
  56.             if ret >= 0 then
  57.                 pos := ret
  58.             else if error = noErr then
  59.                 error := ret;
  60.         end;
  61.     end;
  62.  
  63.     function ReadFromHandleFile (var hf: HandleFile; var s: str255): boolean;
  64.         var
  65.             orgoffset, size: longInt;
  66.             p, q: ptr;
  67.             len: integer;
  68.     begin
  69.         with hf do begin
  70.             size := GetHandleSize(data);
  71.             ReadFromHandleFile := pos < size;
  72.             orgoffset := pos;
  73.             p := ptr(ord(data^) + pos);
  74.             q := p;
  75.             while (pos < size) & (p^ <> 13) & (p^ <> 10) do begin
  76.                 pos := pos + 1;
  77.                 longInt(p) := longInt(p) + 1;
  78.             end;
  79.             len := pos - orgoffset;
  80.             if len > 255 then
  81.                 len := 255;
  82. {$PUSH}
  83. {$R-}
  84.             s[0] := chr(len);
  85.             BlockMove(q, @s[1], len);
  86. {$POP}
  87.             if (pos < size) & (p^ = 13) then begin
  88.                 pos := pos + 1;
  89.                 longInt(p) := longInt(p) + 1;
  90.             end;
  91.             if (pos < size) & (p^ = 10) then begin
  92.                 pos := pos + 1;
  93.                 longInt(p) := longInt(p) + 1;
  94.             end;
  95.         end;
  96.     end;
  97.  
  98. end.